<?php session_start();
require_once "functions.php";
$id = $_SESSION['member_id'];
if (isset($_POST['member_id']) && isset($_POST['message_id'])) {
    $message_id = mysql_real_escape_string($_POST['message_id']);
    mysql_query("UPDATE `messages` SET `seen` = 1 WHERE message_id={$message_id}") or die(mysql_error());
    $num_messages = mysql_query("SELECT COUNT(m.message_id) as cnt FROM messages m INNER JOIN member_message_member mmm ON (m.message_id = mmm.message_id)
                    WHERE mmm.member_id2 = $id AND m.seen = 0") or die(mysql_error());
    $Mcount = mysql_fetch_assoc($num_messages);
    $count = $Mcount['cnt'];
    echo $count;
} else {
    $num_messages = mysql_query("SELECT COUNT(m.message_id) as cnt FROM messages m INNER JOIN member_message_member mmm ON (m.message_id = mmm.message_id)
                    WHERE mmm.member_id2 = $id AND m.seen = 0") or die(mysql_error());
$Mcount = mysql_fetch_assoc($num_messages);
$count = $Mcount['cnt'];
echo $count;
}
?>
